package com.cobo.fitness;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;

import java.io.File;
import java.util.Arrays;
import java.util.Locale;

public class CodeCreator {


    String[] modules = new String[]{
            "entity",
            "mapper",
            "routes",
            "services"
    };

    private static String updateHead(String name) {
        String suffix = StringUtils.substring(name, 1);
        String head = StringUtils.substring(name, 0, 1).toUpperCase(Locale.ROOT);
        String clazName = head + suffix;
        return clazName;
    }


    public static void creator(String name,String baseDir) throws Exception{
        FileUtils.forceMkdir(new File(baseDir));
        String[] dirArr = baseDir.split("\\\\");
        String basePackage = "";
        boolean open = false;
        for (String s : dirArr) {
            if (open) {
                basePackage += s + ".";
            }
            if (s.equals("java")) {
                open = true;
            }
        }


        String[] segments = name.split("_");
        String entityName = Arrays.stream(segments).map(CodeCreator::updateHead).reduce((t1, t2) -> t1 + t2).get();

        FileUtils.forceMkdir(new File(baseDir, "entity"));
        FileUtils.forceMkdir(new File(baseDir, "mapper"));
        FileUtils.forceMkdir(new File(baseDir, "routes"));
        FileUtils.forceMkdir(new File(baseDir, "services"));
        FileUtils.forceMkdir(new File(baseDir, "services/impl"));

        String entityPkg = basePackage + "entity";
        String mapperPkg = basePackage + "mapper";
        String servicePkg = basePackage + "services";
        String simpl = basePackage + "services.impl";
        StringBuilder sb = new StringBuilder();
        sb.append("package "+entityPkg+";\n")
                .append("import com.baomidou.mybatisplus.annotation.IdType;\n" +
                        "import com.baomidou.mybatisplus.annotation.TableId;\n" +
                        "import lombok.Data;\n" +
                        "\n" +
                        "import javax.persistence.Table;\n")
                .append("@Table(name = \""+name+"\")\n")
                .append("@Data\n")
                .append("public class "+entityName+" {\n")
                .append("\t@TableId(type = IdType.ASSIGN_ID)\n")
                .append("\tprivate String id;\n}")
        ;
        String entityContent = sb.toString();



        FileUtils.writeStringToFile(new File(baseDir, "entity/"+entityName + ".java"), entityContent, "utf-8");

        sb = new StringBuilder();
        sb.append("package " + mapperPkg + ";\n")
                .append("import " + entityPkg + "." + entityName + ";\n")
                .append("\n" +
                        "import com.baomidou.mybatisplus.core.mapper.BaseMapper;\n")
                .append("public interface " + entityName + "Mapper extends BaseMapper<" + entityName + "> {\n")
                .append("}");
        FileUtils.writeStringToFile(new File(baseDir, "mapper/"+entityName + "Mapper.java"), sb.toString(), "utf-8");

        sb = new StringBuilder();
        sb.append("package "+servicePkg+";\n")
                .append("import " + entityPkg + "." + entityName + ";\n")
                .append("import com.baomidou.mybatisplus.extension.service.IService;\n")
                .append("public interface "+entityName+"Service  extends IService<"+entityName+"> {\n}");

        FileUtils.writeStringToFile(new File(baseDir, "services/"+entityName + "Service.java"), sb.toString(), "utf-8");
        sb = new StringBuilder();
        sb.append("package "+simpl+";\n")
                .append("import "+servicePkg+"."+entityName+"Service;\n")
                .append("import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;\n")
                .append("import " + entityPkg + "." + entityName + ";\n")
                .append("import " + mapperPkg + "." + entityName + "Mapper;\n")
                .append("import org.springframework.beans.factory.annotation.Autowired;\n" +
                        "import org.springframework.stereotype.Service;\n")
                .append("@Service\n")
                .append("public class "+entityName+"ServiceImpl  extends ServiceImpl<"+entityName+"Mapper, "+entityName+"> implements "+entityName+"Service {\n")
                .append("}")
        ;
        FileUtils.writeStringToFile(new File(baseDir, "services/impl/"+entityName + "ServiceImpl.java"), sb.toString(), "utf-8");
    }

    public static void main(String[] args)throws Exception {
        String baseDir = "D:\\code\\workstation\\server\\calibre-api\\api\\src\\main\\java\\com\\cobo\\fitness\\web\\calibreapi";
        CodeCreator.creator("Library", baseDir);

    }
}
